using System;
using LiveSequence.Common.Context;
using LiveSequence.Common.Domain;
using System.Collections.Generic;

namespace LiveSequence.Common.Presentation
{
  public class WPFRenderer : IRenderer
  {
    public string Export(SequenceData data)
    {
      RenderDiagram(data);
      return string.Empty;
    }

    public void RenderDiagram(SequenceData data)
    {
      using (SequenceContextScope renderingScope = ContextHelper.CreateSequenceScope())
      {
        //// add all the objects to the scope's context.
        //foreach (var item in data.GetObjectList())
        //{
        //  if (item.Visible)
        //  {
        //  renderingScope.CurrentContext.AddSequenceObject(item.Key, item.TypeName);
        //}
        //}

        List<ObjectInstanceInfo> lst = data.GetObjectList();
        renderingScope.CurrentContext.AddSequenceObject(lst[0].Key, lst[0].TypeName);

        int iCount = 0;
        // add all the messages to the scope's context.
        foreach (var item in data.GetMethodList())
        {
          MethodCallInfo itemCopy = item;

          ObjectInstanceInfo source =
            data.GetObjectList().Find(
              objectInfo => objectInfo.TypeName.Equals(itemCopy.TypeName, StringComparison.Ordinal));
          ObjectInstanceInfo target =
            data.GetObjectList().Find(
              objectInfo => objectInfo.TypeName.Equals(itemCopy.MethodCallType, StringComparison.Ordinal));

          if (source.Visible && target.Visible && itemCopy.Visible)
          {

            renderingScope.CurrentContext.AddSequenceObject(source.Key, source.TypeName);
            renderingScope.CurrentContext.AddSequenceObject(target.Key, target.TypeName);

            renderingScope.CurrentContext.AddMessage(source.Key, target.Key, item.MethodCallName, item);
            iCount++;          

          }

          if(iCount > 100)
          {
            break;
          }
        }

        // render the diagram.
        renderingScope.CurrentContext.Render();
      }
    }
  }
}